import 'package:flutter/cupertino.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:hitutor/UI/NoticeAndMessage/DialogReadNotice.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/Check.dart';
import 'package:hitutor/Widget/ChooserBox.dart';
import 'package:hitutor/Widget/MyBgPage.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';

import '../../Util/HttpGo.dart';

class GroupClassPay extends StatefulWidget {
  dynamic data;

  Function() onFinish;
  GroupClassPay(this.data,{required this.onFinish});

  @override
  State<StatefulWidget> createState() {
    return _GroupClassPay();
  }
}

class _GroupClassPay extends State<GroupClassPay> {
  int backDay = 0;
  List<String> backDays = [];

  bool agree = false;

  chooseBackDay() {
    showWheelDialog(context, "", backDays, (index) {
      backDay = index;
      setState(() {});
    });
  }

  readBuyRule() {
    showWidgetDialog(context, BuyTerm(widget.data["contractContext"]));
  }

  pay() {
    if (!agree) {
      showToast("請先閱讀並同意相關購買合約");
      return;
    }
    HttpGo(context).post("/GroupLesson/BuyGroupLesson", {"PayDay": backDay + 1, "courseId": widget.data["courseId"]}, (data) {
      PageUtil().launchURL(data["payUrl"]); //付款
    }, (code, error) => showToast(error));
  }

  @override
  void initState() {
    for (int i = 1; i <= 28; i++) {
      backDays.add("$i 號");
    }
    super.initState();
  }

  @override
  void deactivate() {
    widget.onFinish.call();
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    return MyBgPage(
        "團班課程 - 付款",
        Container(
            margin: EdgeInsets.fromLTRB(16, 16, 16, 24),
            padding: EdgeInsets.fromLTRB(16, 16, 16, 16),
            decoration: Global.getWhiteBg_shadow(8, MyColor.DD),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(children: [
                  Text("每月扣款日期 : ", style: TextStyle(fontSize: 14, color: MyColor.Green)),
                  SizedBox(width: 100, height: 32, child: ChooserBox("${backDay + 1} 號", onTap: chooseBackDay))
                ]),
                Text("購買項目 : ", style: TextStyle(fontSize: 14, color: MyColor.Green)),
                Text("${widget.data["courseId"]}-${widget.data["courseName"]}",
                    style: TextStyle(fontSize: 14, height: 1.5, color: MyColor.C7D)),
                Container(height: 1, color: Color(0xffcccccc), margin: EdgeInsets.fromLTRB(0, 16, 0, 8)),
                Container(
                    alignment: Alignment.centerRight,
                    child: Text("${widget.data["price"]} 元", style: TextStyle(fontSize: 14, color: MyColor.C7D))),
                Row(children: [
                  GestureDetector(
                      onTap: () {
                        agree = !agree;
                        setState(() {});
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Check(agree)),
                  Container(width: 6),
                  Text("我同意", style: TextStyle(fontSize: 14, color: MyColor.C7D)),
                  TextBtn("相關購買合約", style: TextStyle(fontSize: 14, color: Color(0xff0d6efd)), onTap: readBuyRule),
                  Text("。", style: TextStyle(fontSize: 14, color: MyColor.C7D)),
                ]),
                Container(height: 24),
                Button("信用卡付款", radius: 4, isBorderBg: true, bgColor: MyColor.Orange, width: double.infinity, height: 32, onTap: pay)
              ],
            )),
        bg2: true);
  }
}

class BuyTerm extends StatelessWidget {
  String term = "";

  BuyTerm(this.term);

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: Global.getWhiteBg(8),
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            Container(
                height: 40,
                color: MyColor.Orange,
                alignment: Alignment.center,
                child: Text("購買合約", style: TextStyle(fontSize: 22, color: MyColor.White, fontWeight: FontWeight.bold))),
            Container(height: 12),
            Expanded(child: SingleChildScrollView(child: HtmlWidget(term))),
            Container(height: 12),
            Button("確認", width: 72, height: 32, bgColor: MyColor.Orange, onTap: () {
              PageUtil().back(context);
            })
          ],
        ));
  }
}
